﻿using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class MenuController : MonoBehaviour
{
    GameObject menuPanel;
    RectTransform reset;        // reset

    Animator titleAnim;
    Animator resetAnim;

    void Start()
    {

        Transform[] children = transform.GetComponentsInChildren<Transform>();

        foreach(Transform child in children)
        {
            if (child.name == "Menu")
            {
                menuPanel = child.gameObject;
            }
            else if (child.name == "Title")
            {
                titleAnim = child.GetComponent<Animator>();
            }
            else if (child.name == "ResetBtn")
            {
                reset = child.GetComponent<RectTransform>();
                resetAnim = child.GetComponent<Animator>();
            }
        }

        reset.GetComponent<Button>().onClick.AddListener(OnResetButtonClick);

        HideMenu();
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (menuPanel.activeSelf)
            {
                HideMenu();
            }
            else
            {
                ShowMenu();
            }
        }
    }

    private void OnDestroy()
    {
        if (reset != null)
        {
            reset.GetComponent<Button>().onClick.RemoveListener(OnResetButtonClick);
        }
    }


    public void ShowMenu()
    {
        menuPanel.SetActive(true);

        titleAnim.Play("Title_show");
        resetAnim.Play("Reset_show");

        Time.timeScale = 0;
    }

    public void HideMenu()
    {
        menuPanel.SetActive(false);
        Time.timeScale = 1;

    }


    public void OnResetButtonClick()
    {
        GameManager.Instance.ResetScene();
    }
}
